﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Text;
using System.Data;
using System;

public class SceneToXMLEditor : Editor
{
    private static DataTable table;
    private static DataTable DefaultSceneTable;
    //将所有游戏场景导出为XML格式//
    [MenuItem("GameObject/ExportSceneToXML")]
    static void ExportXML()
    {

        //遍历所有的游戏场景(可以通过点选打包场景来控制)//
        foreach (UnityEditor.EditorBuildSettingsScene S in UnityEditor.EditorBuildSettings.scenes)
        {
            //当关卡启用//
            if (S.enabled)
            {
                try
                {
                    //得到关卡的名称//
                    string ScenePath = S.path;
                    //打开这个关卡//
                    EditorApplication.OpenScene(ScenePath);
                    string[] defaultName = ScenePath.Split(new string[] { "/", "." }, StringSplitOptions.RemoveEmptyEntries);

                    //////////////////////////////////////////////////////////////////////////
                    
                    table = new DataTable(defaultName[1]);


                    DataColumn column = new DataColumn("ID(type:refid)", typeof(System.String));
                    //column.AutoIncrement = true;
                    table.Columns.Add(column);

                    column = new DataColumn("Type(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);

                    column = new DataColumn("IdIndex(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
					
					column = new DataColumn("NeedInit(type:int32)", typeof(System.Boolean));
                    table.Columns.Add(column);

                    column = new DataColumn("PrefabName(type:string)", typeof(System.String));
                    table.Columns.Add(column);

                    column = new DataColumn("ScaleX(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
                    column = new DataColumn("ScaleY(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
                    

                    column = new DataColumn("PosX(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
                    column = new DataColumn("PosY(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);

                    column = new DataColumn("ColliderPosX(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
                    column = new DataColumn("ColliderPosY(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
                    column = new DataColumn("ColliderScaleX(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
                    column = new DataColumn("ColliderScaleY(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);

                    column = new DataColumn("FrameSpeed(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);

                    column = new DataColumn("NamePosX(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
                    column = new DataColumn("NamePosY(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);

                    column = new DataColumn("Name(type:string)", typeof(System.String));
                    table.Columns.Add(column);

                    column = new DataColumn("ShadowPosX(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
                    column = new DataColumn("ShadowPosY(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
                    column = new DataColumn("ShadowScaleX(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
                    column = new DataColumn("ShadowScaleY(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);

                    column = new DataColumn("TaskStatusPosX(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
                    column = new DataColumn("TaskStatusPosY(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);



                    column = new DataColumn("Speed(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
                    column = new DataColumn("ReapeatTime(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
                    column = new DataColumn("WalkRadius(type:int32)", typeof(System.Int32));//扩大10倍记住读取时除以10//
                    table.Columns.Add(column);
					
					column = new DataColumn("AniPosX(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);
					column = new DataColumn("AniPosY(type:int32)", typeof(System.Int32));
                    table.Columns.Add(column);

                    //column = new DataColumn("UpPointX(type:int32)", typeof(System.Int32));
                    //table.Columns.Add(column);
                    //column = new DataColumn("UpPointY(type:int32)", typeof(System.Int32));
                    //table.Columns.Add(column);

                    //column = new DataColumn("MidPointX(type:int32)", typeof(System.Int32));
                    //table.Columns.Add(column);
                    //column = new DataColumn("MidPointY(type:int32)", typeof(System.Int32));
                    //table.Columns.Add(column);

                    //column = new DataColumn("DownPointX(type:int32)", typeof(System.Int32));
                    //table.Columns.Add(column);
                    //column = new DataColumn("DownPointY(type:int32)", typeof(System.Int32));
                    //table.Columns.Add(column);


                    //////////////////////////////////////////////////////////////////////////

                    DefaultSceneTable = new DataTable("Common" + defaultName[1]);

                    DataColumn columnRes = new DataColumn("CommonID(type:string)", typeof(System.String));
                    DefaultSceneTable.Columns.Add(columnRes);

                    columnRes = new DataColumn("CommonPrefabName(type:string)", typeof(System.String));
                    DefaultSceneTable.Columns.Add(columnRes);

                    columnRes = new DataColumn("CommonFile(type:string)", typeof(System.String));
                    DefaultSceneTable.Columns.Add(columnRes);

                    columnRes = new DataColumn("CommonScaleX(type:int32)", typeof(System.Int32));
                    DefaultSceneTable.Columns.Add(columnRes);
                    columnRes = new DataColumn("CommonScaleY(type:int32)", typeof(System.Int32));
                    DefaultSceneTable.Columns.Add(columnRes);
					columnRes = new DataColumn("CommonScaleZ(type:int32)", typeof(System.Int32));
                    DefaultSceneTable.Columns.Add(columnRes);

                    columnRes = new DataColumn("CommonPosX(type:int32)", typeof(System.Int32));
                    DefaultSceneTable.Columns.Add(columnRes);
                    columnRes = new DataColumn("CommonPosY(type:int32)", typeof(System.Int32));
                    DefaultSceneTable.Columns.Add(columnRes);
					columnRes = new DataColumn("CommonPosZ(type:int32)", typeof(System.Int32));
                    DefaultSceneTable.Columns.Add(columnRes);

                    //////////////////////////////////////////////////////////////////////////

                    // Add rows.
                    DataRow row;
                    GameObject parentContainer = GameObject.Find("Panel(NPC)");
					
                    foreach (Transform component in parentContainer.GetComponentsInChildren<Transform>() )
                    {
                        GameObject obj = component.gameObject;
						
                        if (obj.name == "Panel(NPC)" || component.parent != parentContainer.transform) continue;

                        if(obj.name.IndexOf("Npc")!= -1 && obj.tag != "Door")
                        {
                            String[] configArr = obj.name.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);

                            row = table.NewRow();

                            row["ID(type:refid)"] = configArr[0];
                            row["Type(type:int32)"] = configArr[1];
                            row["IdIndex(type:int32)"] = configArr[2];

                            row["PrefabName(type:string)"] = obj.tag;//configArr[1] == "4" ? "Npc" : "Monster";//不同类型，对应不同的预设体//
							
							row["NeedInit(type:int32)"] = obj.activeInHierarchy ? 1 : 0;			//1:true;0:false//

                            row["ScaleX(type:int32)"] = obj.transform.localScale.x;
                            row["ScaleY(type:int32)"] = obj.transform.localScale.y;

                            row["PosX(type:int32)"] = obj.transform.localPosition.x;
                            row["PosY(type:int32)"] = obj.transform.localPosition.y;

                            BoxCollider boxColloder = obj.GetComponent<BoxCollider>();
                            row["ColliderPosX(type:int32)"] = boxColloder.center.x;
                            row["ColliderPosY(type:int32)"] = boxColloder.center.y;

                            row["ColliderScaleX(type:int32)"] = boxColloder.size.x;
                            row["ColliderScaleY(type:int32)"] = boxColloder.size.y;

                            GameObject SpriteAnimationLayer = obj.transform.FindChild("SpriteAnimationLayer").gameObject;
                            row["FrameSpeed(type:int32)"] = SpriteAnimationLayer.GetComponent<ClipAnimation>().FPS;

                            GameObject PlayerName = obj.transform.FindChild("PlayerName").gameObject;
                            row["NamePosX(type:int32)"] = PlayerName.transform.localPosition.x;
                            row["NamePosY(type:int32)"] = PlayerName.transform.localPosition.y;

                            row["Name(type:string)"] = obj.name;

                            GameObject Shadow = obj.transform.FindChild("Shadow").gameObject;
                            row["ShadowPosX(type:int32)"] = Shadow.transform.localPosition.x;
                            row["ShadowPosY(type:int32)"] = Shadow.transform.localPosition.y;

                            row["ShadowScaleX(type:int32)"] = Shadow.transform.localScale.x;
                            row["ShadowScaleY(type:int32)"] = Shadow.transform.localScale.y;

                            Transform TaskTip = obj.transform.FindChild("TaskTip");
							if(TaskTip != null){
	                            row["TaskStatusPosX(type:int32)"] = TaskTip.localPosition.x;
	                            row["TaskStatusPosY(type:int32)"] = TaskTip.localPosition.y;
							} else {
								row["TaskStatusPosX(type:int32)"] = 0;
								row["TaskStatusPosY(type:int32)"] = 0;
							}



                            PlayerAction playerAction = obj.GetComponent<PlayerAction>();
                            row["Speed(type:int32)"] = playerAction != null ? playerAction.speed : 0;
                            row["ReapeatTime(type:int32)"] = playerAction!=null?playerAction.reapeatRate:0;
                            row["WalkRadius(type:int32)"] = playerAction!=null?playerAction.radiusOfWalking * 10:0;//扩大10倍记住读取时除以10//
							
							Transform spriteAnimationLayer = obj.transform.Find("SpriteAnimationLayer");
							if(spriteAnimationLayer != null){
								row["AniPosX(type:int32)"] = spriteAnimationLayer.localPosition.x;
								row["AniPosY(type:int32)"] = spriteAnimationLayer.localPosition.x;
							}

                            //GameObject UpPoint = obj.transform.FindChild("UpPoint").gameObject;
                            //row["UpPointX(type:int32)"] = UpPoint.transform.localPosition.x;
                            //row["UpPointY(type:int32)"] = UpPoint.transform.localPosition.y;

                            //GameObject MidPoint = obj.transform.FindChild("MidPoint").gameObject;
                            //row["MidPointX(type:int32)"] = MidPoint.transform.localPosition.x;
                            //row["MidPointY(type:int32)"] = MidPoint.transform.localPosition.y;

                            //GameObject DownPoint = obj.transform.FindChild("DownPoint").gameObject;
                            //row["DownPointX(type:int32)"] = DownPoint.transform.localPosition.x;
                            //row["DownPointY(type:int32)"] = DownPoint.transform.localPosition.y;

                            table.Rows.Add(row);
                        }
                        else
                        {
                            row = DefaultSceneTable.NewRow();

                            row["CommonID(type:string)"] = obj.name;

                            row["CommonPrefabName(type:string)"] = obj.tag;

                            row["CommonFile(type:string)"] = obj.name=="Background"?obj.GetComponent<MeshRenderer>().sharedMaterial.mainTexture.name:"null";//不同类型，对应不同的资源路径//

                            row["CommonScaleX(type:int32)"] = obj.transform.localScale.x;
                            row["CommonScaleY(type:int32)"] = obj.transform.localScale.y;
							row["CommonScaleZ(type:int32)"] = obj.transform.localScale.z;

                            row["CommonPosX(type:int32)"] = obj.transform.localPosition.x;
                            row["CommonPosY(type:int32)"] = obj.transform.localPosition.y;
							row["CommonPosZ(type:int32)"] = obj.transform.localPosition.z;

                            DefaultSceneTable.Rows.Add(row);
                        }


                        
                    }

                    table.AcceptChanges();
                    DefaultSceneTable.AcceptChanges();

                    //////////////////////////////////////////////////////////////////////////

                    string SavePath = EditorUtility.SaveFilePanel(
                        "Save ExportFile",
                        "",
                        defaultName[1],
                        "xml");


                    if (SavePath.Length != 0)
                    {
                        if (File.Exists(SavePath))
                        {
                            File.Delete(SavePath);
                        }
                        table.WriteXml(SavePath, XmlWriteMode.WriteSchema);

                        EditorUtility.DisplayDialog("Alert!", "Export Scene Success!", "OK");
                    }

                    string SaveCommonPath = EditorUtility.SaveFilePanel(
                        "Save CommonExportFile",
                        "",
                        defaultName[1]+"Common",
                        "xml");


                    if (SaveCommonPath.Length != 0)
                    {
                        if (File.Exists(SaveCommonPath))
                        {
                            File.Delete(SaveCommonPath);
                        }
                        DefaultSceneTable.WriteXml(SaveCommonPath.ToString(), XmlWriteMode.WriteSchema);

                        EditorUtility.DisplayDialog("Alert!", "Export CommonScene Success!", "OK");
                    }

                }
                catch (System.Exception ex)
                {
                    EditorUtility.DisplayDialog("Error!", ex.ToString(), "OK");
                }



                //刷新Project视图， 不然需要手动刷新哦//
                AssetDatabase.Refresh();
            }


        }
    }
}